package com.example.demo.controller;

import com.example.demo.WebSocket.WebSocketServer;
import com.example.demo.pojo.FriendsAndMessage;
import com.example.demo.pojo.Friends;
import com.example.demo.pojo.Message;
import com.example.demo.pojo.SigIn;
import com.example.demo.pojo.User;
import com.example.demo.server.FriendsServer;
import com.example.demo.server.UserServer;
import com.example.demo.utils.TokenTool;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘二朋
 * @date 2020-12-03 14:29
 */
@RestController
@RequestMapping("/friends")
@Tag(name = "用户朋友列表管理", description = "处理用户的朋友信息 , 查询用户朋友的信息 , 删除用户的朋友的信息")
@CrossOrigin(origins = "*")//解决跨域问题
public class FriendsController {

    @Autowired
    UserServer userServer;

    @Autowired
    FriendsServer friendsServer;

    /**
     * 获取指定用户的朋友列表 , 以及 , 所有的聊天消息
     *
     * @param request 要进行操作的用户账号
     * @return 返回查询到的数据
     */
    @GetMapping("/getFriends")
    @ResponseBody
    @Operation(summary = "用户获取朋友列表的信息",
            description = "需要token",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "用户请求参数",
                    content = @Content(
                            mediaType = "application/json",
                            examples = @ExampleObject(
                                    value = "{" +
                                            "\"id\":\"13123456781\"," +
                                            "}"
                            ))),
            responses = {
                    @ApiResponse(
                            responseCode = "2000",
                            description = "搜索成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    examples = @ExampleObject(
                                            value = "{\"code\":2000,\"message\":\"搜索成功！\",\"data\":{" +
                                                    "\"friends\":[{" +
                                                    "\"id\":\"11111111\",\"" +
                                                    "url\":\"http://localhost:8080/admin_server/image/1.jpeg\"}," +
                                                    "\"name\":\"用户1\"" +
                                                    "}," +
                                                    "{" +
                                                    "\"id\":\"22222222\",\"" +
                                                    "url\":\"http://localhost:8080/admin_server/image/1.jpeg\"}," +
                                                    "\"name\":\"用户2\"" +
                                                    "}," +
                                                    "..]" +
                                                    "}"
                                    )
                            )),})
    public Message getFriends(HttpServletRequest request) {
        Message message = new Message();
//        从Token中 , 获取用户的账号的信息 .
        String tokenUserId = TokenTool.getTokenUserId(request);
//        通过用户的账号 , 查询 , 用户的所有的朋友账号
        List<Friends> friends = friendsServer.selectFriendsa(tokenUserId);
//        创建一个集合,  来存放用户的朋友列表
        List<FriendsAndMessage> friendsAndMessages = new ArrayList<>();
//        遍历 , 用户的账号 , 得到每一个用户的基本信息
        friends.forEach(friend -> {
//            查询用户的基本信息 , 并把信息进行封装 , 存放到集合里面,  返回到对应的客户端,  进行展示
            User user = userServer.fromById(friend.getFriendb());
//            获取用户的信息 , 封装成一个对象
            FriendsAndMessage friendsAndMessage = new FriendsAndMessage(user.getId(), user.getAvater(), user.getName());
            friendsAndMessages.add(friendsAndMessage);
        });
        message.setSuccessMessage("朋友列表 , 查询成功 ,").put("friends", friendsAndMessages);
        System.out.println("获取用户的朋友列表 : " + friendsAndMessages.size());
        return message;
    }

    /**
     * 删除指定用户的朋友的接口
     *
     * @param user    要进行操作的用户
     * @param request 当前登录的用户
     * @return 返回删除的结果
     */
    @PostMapping("/delfriend")
    @ResponseBody
    @Operation(summary = "添加请求消息",
            responses = {
                    @ApiResponse(
                            responseCode = "2000",
                            description = "删除好友消息 , 发送成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    examples = @ExampleObject(description = "data数据为：", value = "{\n" +
                                            "  \"code\": 2000,\n" +
                                            "  \"message\": \"请求消息 , 发送成功！\",\n" +
                                            "  \"data\": {\n" +
                                            "    \"user\": {\n" +
                                            "      \"id\": 13123456781,\n" +
                                            "      \"userid\": 13123456782,\n" +
                                            "    },\n" +
                                            "  }\n" +
                                            "}")
                            )),
                    @ApiResponse(responseCode = "2001", description = "好友删除失败 , 删除失败")
            })
    public Message delfriend(@RequestBody SigIn user, HttpServletRequest request) {
//        创建一个消息对象
        Message message = new Message();
        String tokenUserId = TokenTool.getTokenUserId(request);
//        互相 . 删除好友
        int i = friendsServer.deleteFriends(tokenUserId, user.getId());
        if (i <= 0) {
//           删除失败
            message.setErrorMessage("用户删除失败 , 请重试");
            return message;
        }
//        用户删除成功
        message.setSuccessMessage("用户删除成功");
        String str = "{\"category\":\"request\",\"specific\":\"FIVE\",\"id\":\"" + tokenUserId + "\"}";
        WebSocketServer.sendMessageToUser(user.getId(), str); //开始发送消息
        return message;
    }


}
